package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Category;
import com.ericom.service.GenericManager;

public class CategoryAction extends BaseAction {
	private GenericManager<Category, Long> categoryManager;
	private List categorys;
	private Category category;
	private Long id;

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setCategoryManager(
			GenericManager<Category, Long> categoryManager) {
		this.categoryManager = categoryManager;
	}

	public String list() {
		categorys = categoryManager.getAll();
		return SUCCESS;
	}

	public List getCategorys() {
		return categorys;
	}

	public String delete() {
		categoryManager.remove(category.getId());
		this.saveMessage(getText("category.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			category = categoryManager.get(id);
		} else {
			category = new Category();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (category.getId() == null);
		category = categoryManager.save(category);

		String key = (isNew) ? "category.added" : "category.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
